(function($){
	var _origainData = {
		//树数据编码  Code ParentCode OrganizedCode  name
		root: {Code: "370000000000",ParentCode:"-1", OrganizedCode:"370000000000", Name:"山东省公安厅"},//根节点 默认
		nodeList: [],																		//当前所有节点数据		
		count: 0,																			//指针，递归数据源使用
		policationNodeList: [],															//适配dtree插件的数据
		abnormalData: ["371523480000","371523490000","370889000000","371209960000"],		//异常数据，编码和父编码不在一个编码下，导致树结构生成不完整
		adapterData: ["371599140001","371599140002","370881620001","371299100001"],		//适配数据，将此编码适配到父编码下，生成完整的树数据结构口
		filterData: ["0","-1","000"],														//过滤数据，过滤到数据库垃圾数据的节点
		issuedRoot: {},																		//适配dtree的根节点
		taskNode: {},
		keyword: "",
		open: false,
		chkDisabled: false,
		
		init: function(data, taskCode){
			if(!data || data.length == 0) return;
			this.adapter(data);										//适配器
			this.sort();											//排序 ，为了递归方便，减少递归次数
			this.parseDataTree();									//解析生成树结构
			this.freeMemory();										//释放指针
			this.setTaskNode(taskCode);
		},
		setTaskNode: function(code){
			this.taskNode = this.getNodeByCode(code, this.root);
		},
		adapter: function(data){
			for(var k in data){
				if(this.filter(data[k])) continue;
				this.nodeList.push(this.convertDBDataToTreeData(data[k]));
			}
		},
		filter: function(obj){
			return this.filterData.indexOf(obj["dm"]) > -1;
		},
		convertDBDataToTreeData: function(obj){            //将数据库的数据字段转换成本对象中节点对象的字段
			var node = new Object();
			node["OrganizedCode"] = obj["dm"];
			var index = this.abnormalData.indexOf(obj["dm"]);
			if(index > -1) {
				node["Code"] = this.adapterData[index];
			}else{
				node["Code"] = obj["dm"];
			};
			node["Name"] = obj["mc"];
			node["ParentCode"] = obj["fdm"];
			return node;
		},
		parseDataTree: function(){						//递归生成树结构数据，根节点为this.root;
			this.root["childrenNodes"] = this.getChildrenData(this.root);
		},
		freeMemory: function(){
			this.nodeList = [];
			this.count = 0;
		},
		sort: function(){
			this.nodeList.sort(this.compare("Code"));
		},
		compare: function(prop){
			return function(a, b){
				if(a[prop] >= b[prop]){
					return 1;
				}else{
					return -1;
				}
			};
		},
		getParentNodeByNode: function(node){
			if(!node) return;
			if(node["Code"] == this.root["Code"]) return this.root;
			return node["parentNode"];
		},
		getListByCode: function(code){
			return this.getListByNode(this.getNodeByNodeCode(code));
		},
		getTaskTreeList: function(val){
			this.resetPolicationNodeList();
			this.keyword = val;
			this.setOpen(!!val);
			this.setIssuedRoot(this.getParentNodeByNode(this.taskNode));
			this.add(this.issuedRoot, true, true);
			this.parseSieveData(this.taskNode);
			if(!this.isRootNode(this.taskNode) && this.taskNode["nodeList"].length > 0) this.add(this.taskNode);
			this.getBrotherListByNode(this.taskNode);
			this.setOpen(false);
			return this.policationNodeList;
		},
		setOpen: function(flag){
			this.open = flag;
		},
		isRootNode: function(node){
			return this.root["Code"] == node["Code"];
		},
		getBrotherListByNode: function(node){
			if(this.isRootNode(node)) return;
			for(var k in this.issuedRoot["childrenNodes"]){
				var siblingNode = this.issuedRoot["childrenNodes"][k];
				if(node["OrganizedCode"] == siblingNode["OrganizedCode"] || !this.sifter(siblingNode)) continue;
				this.add(siblingNode);
			};
		},
		//递归取当前根节点下的所有叶子节点
		getListByNode: function(node){
			this.resetPolicationNodeList();
			if(!node) return this.policationNodeList;
			this.setIssuedRoot(node);
			this.add(this.issuedRoot, true, false);
			this.loadNodeData(this.issuedRoot);
			return this.policationNodeList;
		},
		setIssuedRoot: function(node){
			this.issuedRoot = node;
		},
		resetPolicationNodeList: function(){
			this.policationNodeList = [];
		},
		add: function(node, open, chkDisabled){
			if(!open) open = this.open;
			if(!chkDisabled) chkDisabled = this.chkDisabled;
			this.setNodeOpen(node, open);
			this.setNodechkDisabled(node, chkDisabled);
			this.policationNodeList.push(this.convertNodeToZtreeNode(node));
		},
		setNodeOpen: function(node, open){
			node["open"] = open;
		},
		setNodechkDisabled: function(node, chkDisabled){
			node["chkDisabled"] = chkDisabled;
		},
		convertNodeToZtreeNode: function(node){
			var ztreeNode = new Object();
			ztreeNode["id"] = node["OrganizedCode"];
			if(this.isIssuedRoot(node)){
				ztreeNode["pId"] = "-1";
			}else{
				ztreeNode["pId"] = node["ParentCode"];
			}
			ztreeNode["chkDisabled"] = node["chkDisabled"];
			ztreeNode["open"] = node["open"];
			ztreeNode["name"] = node["Name"];
			return ztreeNode;
		},
		isIssuedRoot: function(node){
			return node["Code"] == this.issuedRoot["Code"];
		},
		sifter: function(node){
			if(!this.keyword) return true;
			if(node["Name"].indexOf(this.keyword) > -1) return true;
			return false;
		},
		parseSieveData: function (root){
			var nodeList = [];
			var childrenNodes = root["childrenNodes"];
			for(var j = 0; j < childrenNodes.length; j++){
				var node = childrenNodes[j];
				if(node["childrenNodes"].length > 0){
					node = this.parseSieveData(node);
					if(node["nodeList"].length > 0){
						nodeList.push(node);
						this.add(node);
					}else{
						if(this.sifter(node)) {
							nodeList.push(node);
							this.add(node);
						};
					};
				}else{
					if(this.sifter(node)) {
						nodeList.push(node);
						this.add(node);
					};
				};
			}
			root["nodeList"] = nodeList;
			return root;
		},
		getChildrenData: function (root){
			var lists = [];
			for(;this.count < this.nodeList.length;){
				var node = this.nodeList[this.count];
				if(node["Code"] == root["Code"]){
					this.count++;
					continue;
				}
				if(node["ParentCode"] == root["Code"]){
					this.count++;
					node["parentNode"] = root;
					node["childrenNodes"] = this.getChildrenData(node);
					lists.push(node);
				}else{
					return lists;
				};
			}
			return lists;
		},
		getNodeByCode: function (code, root){
			if(root["OrganizedCode"] == code) return root;
			var childrenNodes = root["childrenNodes"];
			if(childrenNodes.length == 0) return;
			for(var k in childrenNodes){
				var node = childrenNodes[k];
				if(node["OrganizedCode"] == code) return node;
				var nodeData = this.getNodeByCode(code, node);
				if(nodeData) return nodeData;
			};
		},
		loadNodeData: function(root){
			var childrenNodes = root["childrenNodes"];
			if(childrenNodes == 0) return;
			for(var j = 0; j < childrenNodes.length; j++){
				var node = childrenNodes[j];
				this.add(node);
				this.loadNodeData(node);
			};
		},
	};
	$.fn.origainData = {
		init: function(data, taskOrigain){
			_origainData.init(data, taskOrigain);
		},
		getOrigainData: function(){
			return _origainData;
		}
	};

	//var data = $("#organizedData").val() ? $("#organizedData").val() : localStorage.getItem("organizedData");
	//if(!data) return;
	//data = eval('(' + res + ')');
   // console.log(res)
	
})(jQuery);



